<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
require_once ('./application/controllers/authenticate.php');

class City extends Authenticate {

    const CITY_PER_PAGE = 100;
    const CITY_PAGE_RANGE = 5;

    function __construct() {
        global $active_location_function;
        $active_location_function = 'admin/location/city/index';
        parent::__construct();
        $this->load->model('location_city_model');
    }

    protected function _eventLoadLayout() {
        $this->page['title'] = 'Quản lý thành phố';
        $this->module['title'] = 'Quản lý thành phố';
    }

    function index() {
        try {
            $data = $this->_initData();
            $data['mode'] = 'grid';
            $this->setPageContent('admin/location/city/grid', $data);
            $this->renderLayout('layouts/fullcolumn');
        } catch (Exception $ex) {
            echo $ex->getMessage();
        }
    }

    protected function _initData() {
        $data['collection'] = $this->location_city_model->get_cities();
        $data['save_action'] = site_url('admin/location/city/save', true);
        return $data;
    }

    function save() {
        try {
            $id = $this->input->post('id');
            $data = $this->input->post('data');
            if (empty($id)) {
                $id = $this->location_city_model->add_city($data);
                $this->st_registry->register('admin/location/city/success', 'Thêm mới thành phố thành công');
            } else {
                $this->location_city_model->update_city($id, $data);
                $this->st_registry->register('admin/location/city/success', 'Cập nhật thông tin thành phố thành công');
            }
            $CI =& get_instance();
            $CI->st_registry->deleteKey('location/cities');
        } catch (Exception $ex) {
            $this->st_registry->register('admin/location/city/error', 'Lỗi xảy ra: ' . $ex->getMessage());
        }
        redirect(site_url('admin/location/city/index', true));
    }

    function ajax_edit() {
        try {
            $id = $this->input->getParam('id');
            if (empty($id)) {
                throw new Exception('Không tìm thấy thành phố theo ID');
                exit();
            }
            $data['city'] = $this->location_city_model->load_by_id($id);
            $data['districts'] = $this->location_city_model->get_districts($id);
            $data['save_action'] = site_url('admin/location/city/save', true);
            $data['mode'] = 'ajax_edit';
            $this->setPageContent('admin/location/city/ajax/edit', $data);
            $this->renderLayout('layouts/ajax');
        } catch (Exception $ex) {
            $this->st_registry->register('admin/location/city/error', 'Lỗi xảy ra: ' . $ex->getMessage());
        }
    }

    function edit() {
        try {
            $id = $this->input->getParam('id');
            if (empty($id)) {
                throw new Exception('Không xác định được thành phố cần sửa đổi thông tin');
                exit();
            }
            $data = $this->_initData();
            $data['city'] = $this->location_city_model->load_by_id($id);
            $data['mode'] = 'edit';
            $this->setPageContent('admin/location/city/grid', $data);
            $this->renderLayout('layouts/fullcolumn');
        } catch (Exception $ex) {
            $this->st_registry->register('admin/location/city/error', 'Lỗi xảy ra: ' . $ex->getMessage());
        }
    }

    function delete() {
        try {
            $id = $this->input->getParam('id');
            if (empty($id)) {
                $this->st_registry->register('admin/location/city/error', 'Không xác định được thành phố cần xóa (ID)?');
                redirect(site_url('admin/location/city/index', true));
                exit();
            }
            $this->location_city_model->delete_city($id);
            $this->st_registry->register('admin/location/city/success', 'Xóa thành phố thành công');
            $CI =& get_instance();
            $CI->st_registry->deleteKey('location/cities');
        } catch (Exception $ex) {
            $this->st_registry->register('admin/location/city/error', 'Lỗi xảy ra: ' . $ex->getMessage());
        }
        redirect(site_url('admin/location/city/index', true));
    }

    function get_districts() {
        $city_id = $this->input->getParam('city_id');
        $data['districts'] = $this->location_city_model->get_districts($city_id);
        $this->setPageContent('admin/location/city/ajax/districts', $data);
        $this->renderLayout('layouts/ajax');
    }

}